/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Elisa
 */
public class Peminjaman {

    private String idPeminjam, idBuku;
    private Date tanggal, tanggalKembali;
//    private int lamaPeminjam;

    public String getIdPeminjam() {
        return idPeminjam;
    }

    public void setIdPeminjam(String idPeminjam) {
        this.idPeminjam = idPeminjam;
    }

    public String getIdBuku() {
        return idBuku;
    }

    public void setIdBuku(String idBuku) {
        this.idBuku = idBuku;
    }

//    public int getLamaPeminjam() {
//        return lamaPeminjam;
//    }
//
//    public void setLamaPeminjam(int lamaPeminjam) {
//        this.lamaPeminjam = lamaPeminjam;
//    }

    public Date getTanggal() {
        return tanggal;
    }

    public void setTanggal(Date tanggal) {
        this.tanggal = tanggal;
    }

    public Date getTanggalKembali() {
        return tanggalKembali;
    }

    public void setTanggalKembali(Date tanggalKembali) {
        this.tanggalKembali = tanggalKembali;
    }

    public void simpanPeminjaman() throws SQLException, Exception {
        PreparedStatement pt = null;
        Connection conn = DatabaseConnection.getConnection();
        java.sql.Date date = new java.sql.Date(this.tanggal.getYear(), this.tanggal.getMonth(), this.tanggal.getDate());

        String sql = "select count(*) from peminjaman where IDMEMBER = ?";
        pt = conn.prepareStatement(sql);
        pt.setString(1, this.idPeminjam);
        ResultSet rs = pt.executeQuery();
        int jum = 0;
        if (rs.next()) {
            jum = rs.getInt(1);
        }

        if (jum >= 3) {
            JOptionPane.showMessageDialog(null, "Anda sudah meminjam lebih dari 3, anda tidak dapat meminjam " + this.idBuku);
            throw new Exception("Sudah lebih dari 3");
        }
        pt.close();
        sql = "insert into peminjaman (IDMEMBER, IDBUKU, TANGGAL) values (?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1, this.idPeminjam);
        pt.setString(2, this.idBuku);
        pt.setDate(3, date);
        pt.executeUpdate();
        rs.close();
        pt.close();
    }

    public void simpanPengembalian() throws SQLException {
        PreparedStatement pT = null;
        Connection conn = DatabaseConnection.getConnection();


        String sql = "select round((sysdate-tanggal)-3) from peminjaman where idmember = ? and idbuku=?";
        pT = conn.prepareStatement(sql);
        pT.setString(1, this.idPeminjam);
        pT.setString(2, this.idBuku);

        ResultSet rs = pT.executeQuery();
        rs.next();
        int x = rs.getInt(1);
        if (x > 0) {
            x = x * 3000;
            JOptionPane.showMessageDialog(null, "Kena denda " + x);
        }

        sql = "delete peminjaman where idmember = ? and idbuku = ?";

        pT = conn.prepareStatement(sql);
        pT.setString(1, this.idPeminjam);
        pT.setString(2, this.idBuku);
        pT.executeUpdate();
    }

    public void updateStock(int stok) throws SQLException {
        PreparedStatement pT = null;
        Connection conn = DatabaseConnection.getConnection();
        String sql = "select STOK from BUKU where IDBUKU = ?";
        pT = conn.prepareStatement(sql);
        pT.setString(1, idBuku);
        ResultSet rs = pT.executeQuery();
        rs.next();
        int tot = rs.getInt(1);
        pT.close();

        tot += stok;

        sql = "update BUKU set stok = ? where idbuku = ?";
        pT = conn.prepareStatement(sql);
        pT.setInt(1, tot);
        pT.setString(2, this.idBuku);
        pT.executeUpdate();
        pT.close();

    }
}
